package ru.rchervot.sanasar.client.view;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;

public class ConnectionInfoViewDefault extends Composite implements ConnectionInfoPresenter.Display {
	private final Label rpcStatus = new Label();
	private final PopupPanel popupPanel = new PopupPanel(false);
	Presenter presenter;
	
	public ConnectionInfoViewDefault() {
		super();
				
		popupPanel.setWidget(rpcStatus);
		popupPanel.hide();
		popupPanel.setStylePrimaryName("DefaultConnectionInfoView");
		
		this.initWidget(new Label(""));
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	private void show() {
		if (popupPanel.isShowing()) return;
		
		popupPanel.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
			public void setPosition(int offsetWidth,
					int offsetHeight) {
				int left = (Window.getClientWidth() - offsetWidth) / 2;
				int top = 0;
				popupPanel.setPopupPosition(left, top);
			}
		});
	}
	
	@Override
	public void hideRPCStatus() {
		popupPanel.hide();
	}



	@Override
	public void showRPCStatusFailure() {
		rpcStatus.setText("Failure!");
		show();
	}



	@Override
	public void showRPCStatusLoading() {
		rpcStatus.setText("Loading...");
		show();
	}
}
